/**
 * weapp-js v2.2.3(2022-4-26)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2022 Motigo, Inc.
 * 
 */
Component({
    data: {
        visible: false,
        cropState: false
    },
    methods: {
        stopEvent() {},

        imageCropInit(opts) {
            return new Promise((resolve, reject) => {
                this.checkImgIsLocal(opts.imgSrc).then(imgSrc => {
                    opts.imgSrc = imgSrc;
                    this.showCropComponent();
                    this.setData({
                        cropState: true
                    });
                    setTimeout(() => {
                        this.cropper = this.selectComponent('#__weapp_ui_image_crop__');
                        this.cropper._init(opts, resolve, reject);
                        this.cropper.imgReset();
                    }, 10);
                });
            });
        },

        checkImgIsLocal(imgSrc) {
            return new Promise((resolve, reject) => {
                if (imgSrc.indexOf('http://tmp/') === -1) {
                    wx.getImageInfo({
                        src: imgSrc,
                        success: result => {
                            resolve(result.path);
                        },
                        fail: err => {
                            reject(err);
                        },
                        complete: () => {}
                    });
                } else {
                    resolve(imgSrc);
                }
            });
        },

        closeCropComponent() {
            this.setData({
                visible: false,
                cropState: false
            });
        },

        showCropComponent() {
            this.setData({
                visible: true
            });
        },
        login(opts) {
            this.setData({
                visible: true
            });
            return new Promise((resolve, reject) => {
                wx.nextTick(() => {
                    this.setData({
                        loginVisible: true
                    });
                    const node = this.selectComponent('#__weapp_ui_login__');
                    if (node) {
                        node.login(opts).then(
                            data => {
                                this.setData({
                                    visible: false,
                                    loginVisible: false
                                });
                                resolve(data);
                            },
                            err => {
                                reject(err);
                                this.setData({
                                    visible: false,
                                    loginVisible: false
                                });
                            }
                        );
                    } else {
                        this.setData({
                            visible: false,
                            loginVisible: false
                        });
                        reject();
                    }
                });
            });
        }
    }
});
